<?php
/*
 * 返利管理
 */

namespace app\adminbackstage\controller;
use app\BaseController;
use think\facade\Request;
use app\adminbackstage\model\Rebate;
use app\adminbackstage\model\Order;
use app\adminbackstage\model\Sold;
use app\adminbackstage\model\Product;
use app\adminbackstage\model\Operation;
use app\common\model\UserAmount;
use app\common\model\Detailed;

use think\facade\Db;

class RebateController extends BaseController{

    //返利列表
    public function rebateList(){
        $a_uid = Request::param('a_uid');
        $page = Request::param('page');
        $num = Request::param('num');
        
        $p_name = Request::param('p_name');
        $type = Request::param('type');

        $where = [];
        $where[] = ['r.enable','=','1'];
        if(!empty($p_name)){
            $where[] = ['so.p_name','like','%'.$p_name.'%'];
        }
        /*if(!empty($type)){
            $where[] = ['type','=',$type];
        }*/
        $list = Rebate::rebateList($where,$page,$num);
        if(!empty($list)){
            $list = $list->toArray();
            foreach ($list as $key => $value) {
                $list[$key]["addtime"] = date( "Y-m-d H:i:s",$value["addtime"]);
                
                    /*if(strpos($value['img'],"img.alicdn.com") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    elseif(strpos($value['img'],"http") !== false){
                        $storeList[$key]["img"] =  $value['img'];
                    }
                    else{
                        
                        $storeList[$key]["img"] =  Config::get('app.swimming_url').$value['img'];
                    }*/
            }
        }
        $count = Rebate::rebateCount($where);
        return json(['code' => 1, 'msg' => lang(''), 'data' => $list, 'count' => $count]);
    }
    
    //仓库添加
    /*public function warehouseAdd(){
        $store = new Store();
        $a_uid = Request::param('a_uid');
        $enable = Request::param('enable');
        $w_name = Request::param('w_name');
        $type = Request::param('type');
        $province = Request::param('province');
        $city = Request::param('city');
        $district = Request::param('district');
        $address = Request::param('address');
        
        if(empty($enable)){
            $enable = 1;
        }
        if($enable == -1){
            $enable  = 0;
        }
        $time = time();
        $data = [

            'w_name' => $w_name,
            'enable' => $enable,
            'addtime' => $time,
            'type' => $type,
            'province' => $province,
            'city' => $city,
            'address' => $address,
            'district' => $district,
            'address' => $address,

        ];

        Warehouse::warehouseAdd($data);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }*/
    
    //返利详情
    public function rebateInfo(){
        $a_uid = Request::param('a_uid');

        $id = Request::param('id');
  
        $where = [];
        $where[] = ['so.id','=',$id];

        $info = Rebate::rebateInfo($where);
        if(!empty($info)){
            $info = $info->toArray();

            $info["addtime"] = date( "Y-m-d H:i:s",$info["addtime"]);

            /*if(strpos($storeList['img'],"img.alicdn.com") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            elseif(strpos($storeList['img'],"http") !== false){
                $storeList["img"] =  $storeList['img'];
            }
            else{

                $storeList["img"] =  Config::get('app.swimming_url').$storeList['img'];
            }*/
       
        }

        return json(['code' => 1, 'msg' => lang(''), 'data' => $info, 'count' => '' ]);
    }
    
    //返利修改
    public function rebateEdit(){

        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $enable = Request::param('enable');
        $rebate_num = Request::param('rebate_num');
        $frequency = Request::param('frequency');

        
        $update = [];
        if(!empty($rebate_num)){
            $update['rebate_num'] = $rebate_num;
        }
        
        if(!empty($frequency)){
            $update['frequency'] = $frequency;
        }
        
        if($enable==-1){
            $update['enable'] = 0;
        }else{
            $update['enable'] = 1;
        }


        Rebate::rebateEdit($id,$update);
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }
    
    //返利执行
    public function  rebateExecute(){
        $a_uid = Request::param('a_uid');
        $id = Request::param('id');
        $state = Request::param('state');
        
        $where = [];
        $where[] = ['so.id','=',$id];

        $info = Rebate::rebateInfo($where);
        if($info['state']==1 && $state==2 ){
            $where_s = [];
            $where_s[] = ['r_id','=',$info['id']];
            $where_s[] = ['state','=',2];
            $where_s[] = ['rebate','=',1];
            $count = Sold::soldCount($where_s);
            if($count >= $info["frequency"]){
                $userAmount = UserAmount::userAmountInfo(["uid"=>$info["uid"]]);

                Db::startTrans();
                try {
                    $time = time();

                    $detailed[] = \detailed($info["uid"],$info["rebate_price"],'rebate_admin',$userAmount['balance'],0);
                    Detailed::detailedAddAll($detailed);
                    UserAmount::userAmountInc(["uid"=>$info["uid"]],"balance",$info["rebate_price"]);
                    
                    $update_r['state']=2;
                    Rebate::rebateEdit($id,$update_r);
                    
                    $update_s['rebate']=2;
                    Sold::soldEdit($id,$update_s);
                    
                    
                    $operation = new Operation();

                    $data = [
                            'uid' => $a_uid,
                            'operation' => '返利发放---'.$state,
                            'main_table' => 'pg_rebate',
                            'mt_id' => $id,
                            'addtime' => $time,
                        ];
                    $operation->operationAdd($data);
                    Db::commit();
                }catch (\Exception $e){
                    Db::rollback();
                }
                
            }
            
        }
        return json(['code' => 1, 'msg' => lang(''), 'data' => '', 'count' => '']);
    }

}
